﻿using System;
using System.Collections.Generic;
using System.Text;
using QuanLyKho.Common;
using System.Data.SqlClient;

namespace QuanLyKho.DataAccess
{
    public class clsDataAccessListSupplier : iAction
    {
        private SqlConnection conn = new clsConnect().GetSqlConnection();
        private SqlCommand cmd = null;
        private SqlDataReader dr = null;
        private StringBuilder sBuilder = null;

        #region iAction Members
        /// <summary>
        /// Them nha cung cap
        /// Authur : DanhtruongIT
        /// </summary>
        /// <param name="objParam"></param>
        /// <returns></returns>
        public int AddNew(object objParam)
        {
            try
            {
                int status = 0;
                conn.Open();
                clsListSupplier obj = (clsListSupplier)objParam;
                sBuilder = new StringBuilder("Sp_ListSupplier_Add");
                cmd = new SqlCommand(sBuilder.ToString(), conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Code", obj.Code);
                cmd.Parameters.AddWithValue("@Name", obj.Name);
                cmd.Parameters.AddWithValue("@Des", obj.Des);
                cmd.Parameters.AddWithValue("@Address", obj.Address);
                cmd.Parameters.AddWithValue("@Tel", obj.Tel);
                cmd.Parameters.AddWithValue("@Fax", obj.Fax);
                cmd.Parameters.AddWithValue("@Website", obj.Website);
                cmd.Parameters.AddWithValue("@Email", obj.Email);
                cmd.Parameters.AddWithValue("@Manager", obj.Manager);
                cmd.Parameters.AddWithValue("@Position", obj.Position);
                cmd.Parameters.AddWithValue("@ContactTel", obj.ContactTel);
                cmd.Parameters.AddWithValue("@ContactFax", obj.ContactFax);
                cmd.Parameters.AddWithValue("@ContactEmail", obj.ContactEmail);

                status = cmd.ExecuteNonQuery();
                conn.Close();
                return status;
            }
            catch (Exception ex)
            { throw ex; }
            finally
            { conn.Close(); }
        }
        public int Update(object objParam)
        {
            try
            {
                int status = 0;
                conn.Open();
                clsListSupplier obj = (clsListSupplier)objParam;
                sBuilder = new StringBuilder("Sp_ListSupplier_Update");
                cmd = new SqlCommand(sBuilder.ToString(), conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ID", obj.ID);
                cmd.Parameters.AddWithValue("@Code", obj.Code);
                cmd.Parameters.AddWithValue("@Name", obj.Name);
                cmd.Parameters.AddWithValue("@Des", obj.Des);
                cmd.Parameters.AddWithValue("@Address", obj.Address);
                cmd.Parameters.AddWithValue("@Tel", obj.Tel);
                cmd.Parameters.AddWithValue("@Fax", obj.Fax);
                cmd.Parameters.AddWithValue("@Website", obj.Website);
                cmd.Parameters.AddWithValue("@Email", obj.Email);
                cmd.Parameters.AddWithValue("@Manager", obj.Manager);
                cmd.Parameters.AddWithValue("@Position", obj.Position);
                cmd.Parameters.AddWithValue("@ContactTel", obj.ContactTel);
                cmd.Parameters.AddWithValue("@ContactFax", obj.ContactFax);
                cmd.Parameters.AddWithValue("@ContactEmail", obj.ContactEmail);
                status = cmd.ExecuteNonQuery();
                conn.Close();
                return status;
            }
            catch (Exception ex)
            { throw ex; }
            finally
            { conn.Close(); }
        }

        public int Delete(long id)
        {
            try
            {
                int status = 0;
                conn.Open();
                sBuilder = new StringBuilder("Sp_ListSupplier_Delete");
                cmd = new SqlCommand(sBuilder.ToString(), conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ID", id);
                status = cmd.ExecuteNonQuery();
                conn.Close();
                return status;
            }
            catch (Exception ex)
            { throw ex; }
            finally
            { conn.Close(); }
        }

        #endregion
        public List<clsListSupplier> ListSupplierSelectAll()
        {
            try
            {
                List<clsListSupplier> list = new List<clsListSupplier>();
                sBuilder = new StringBuilder("Sp_ListSupplier_SelectAll");
                conn.Open();
                cmd = new SqlCommand(sBuilder.ToString(), conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        clsListSupplier obj = new clsListSupplier();
                        if (dr["ID"] != DBNull.Value)
                            obj.ID = Convert.ToInt64(dr["ID"]);
                        if (dr["Code"] != DBNull.Value)
                            obj.Code = Convert.ToString(dr["Code"]);
                        if (dr["Name"] != DBNull.Value)
                            obj.Name = Convert.ToString(dr["Name"]);
                        if (dr["Des"] != DBNull.Value)
                            obj.Des = Convert.ToString(dr["Des"]);
                        if (dr["Address"] != DBNull.Value)
                            obj.Address = Convert.ToString(dr["Address"]);
                        if (dr["Tel"] != DBNull.Value)
                            obj.Tel = Convert.ToString(dr["Tel"]);
                        if (dr["Fax"] != DBNull.Value)
                            obj.Fax = Convert.ToString(dr["Fax"]);
                        if (dr["Website"] != DBNull.Value)
                            obj.Website = Convert.ToString(dr["Website"]);
                        if (dr["Email"] != DBNull.Value)
                            obj.Email = Convert.ToString(dr["Email"]);
                        if (dr["Manager"] != DBNull.Value)
                            obj.Manager = Convert.ToString(dr["Manager"]);
                        if (dr["Position"] != DBNull.Value)
                            obj.Position = Convert.ToString(dr["Position"]);
                        if (dr["ContactTel"] != DBNull.Value)
                            obj.ContactTel = Convert.ToString(dr["ContactTel"]);
                        if (dr["ContactFax"] != DBNull.Value)
                            obj.ContactFax = Convert.ToString(dr["ContactFax"]);
                        if (dr["ContactEmail"] != DBNull.Value)
                            obj.ContactEmail = Convert.ToString(dr["ContactEmail"]);
                        list.Add(obj);
                    }
                }
                conn.Close();
                return list;
            }
            catch (Exception ex)
            { throw ex; }
            finally
            { conn.Close(); }
        }

        public List<clsListSupplier> SelectAllWithName(string Name)
        {
            try
            {
                List<clsListSupplier> list = new List<clsListSupplier>();
                sBuilder = new StringBuilder("Sp_ListSupplier_SelectByName");
                conn.Open();
                cmd = new SqlCommand(sBuilder.ToString(), conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        clsListSupplier obj = new clsListSupplier();
                        if (dr["ID"] != DBNull.Value)
                            obj.ID = Convert.ToInt64(dr["ID"]);
                        if (dr["Code"] != DBNull.Value)
                            obj.Code = Convert.ToString(dr["Code"]);
                        if (dr["Name"] != DBNull.Value)
                            obj.Name = Convert.ToString(dr["Name"]);
                        if (dr["Des"] != DBNull.Value)
                            obj.Des = Convert.ToString(dr["Des"]);
                        if (dr["Address"] != DBNull.Value)
                            obj.Address = Convert.ToString(dr["Address"]);
                        if (dr["Tel"] != DBNull.Value)
                            obj.Tel = Convert.ToString(dr["Tel"]);
                        if (dr["Fax"] != DBNull.Value)
                            obj.Fax = Convert.ToString(dr["Fax"]);
                        if (dr["Website"] != DBNull.Value)
                            obj.Website = Convert.ToString(dr["Website"]);
                        if (dr["Email"] != DBNull.Value)
                            obj.Email = Convert.ToString(dr["Email"]);
                        if (dr["Manager"] != DBNull.Value)
                            obj.Manager = Convert.ToString(dr["Manager"]);
                        if (dr["Position"] != DBNull.Value)
                            obj.Position = Convert.ToString(dr["Position"]);
                        if (dr["ContactTel"] != DBNull.Value)
                            obj.ContactTel = Convert.ToString(dr["ContactTel"]);
                        if (dr["ContactFax"] != DBNull.Value)
                            obj.ContactFax = Convert.ToString(dr["ContactFax"]);
                        if (dr["ContactEmail"] != DBNull.Value)
                            obj.ContactEmail = Convert.ToString(dr["ContactEmail"]);
                        list.Add(obj);
                    }
                }
                conn.Close();
                return list;
            }
            catch (Exception ex)
            { throw ex; }
            finally
            { conn.Close(); }
        }


        public List<clsListSupplier> ListSupplierSelectOneWithId(long id)
        {
            try
            {
                List<clsListSupplier> list = new List<clsListSupplier>();
                sBuilder = new StringBuilder("Sp_ListSupplier_SelectOne");
                conn.Open();
                cmd = new SqlCommand(sBuilder.ToString(), conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ID", id);
                dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        clsListSupplier obj = new clsListSupplier();
                        if (dr["ID"] != DBNull.Value)
                            obj.ID = Convert.ToInt64(dr["ID"]);
                        if (dr["Code"] != DBNull.Value)
                            obj.Code = Convert.ToString(dr["Code"]);
                        if (dr["Name"] != DBNull.Value)
                            obj.Name = Convert.ToString(dr["Name"]);
                        if (dr["Des"] != DBNull.Value)
                            obj.Des = Convert.ToString(dr["Des"]);
                        if (dr["Address"] != DBNull.Value)
                            obj.Address = Convert.ToString(dr["Address"]);
                        if (dr["Tel"] != DBNull.Value)
                            obj.Tel = Convert.ToString(dr["Tel"]);
                        if (dr["Fax"] != DBNull.Value)
                            obj.Fax = Convert.ToString(dr["Fax"]);
                        if (dr["Website"] != DBNull.Value)
                            obj.Website = Convert.ToString(dr["Website"]);
                        if (dr["Email"] != DBNull.Value)
                            obj.Email = Convert.ToString(dr["Email"]);
                        if (dr["Manager"] != DBNull.Value)
                            obj.Manager = Convert.ToString(dr["Manager"]);
                        if (dr["Position"] != DBNull.Value)
                            obj.Position = Convert.ToString(dr["Position"]);
                        if (dr["ContactTel"] != DBNull.Value)
                            obj.ContactTel = Convert.ToString(dr["ContactTel"]);
                        if (dr["ContactFax"] != DBNull.Value)
                            obj.ContactFax = Convert.ToString(dr["ContactFax"]);
                        if (dr["ContactEmail"] != DBNull.Value)
                            obj.ContactEmail = Convert.ToString(dr["ContactEmail"]);
                        list.Add(obj);
                    }
                }
                conn.Close();
                return list;
            }
            catch (Exception ex)
            { throw ex; }
            finally
            { conn.Close(); }
        }
    }
}
